﻿Imports System.Data.OleDb
Partial Public Class userProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Dim doesUserExistCmd = "SELECT * FROM Users WHERE UserName='" & User.Identity.Name & "'"
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
            oleDbConn.Open()
            Dim dbcmd As OleDbCommand = New OleDbCommand(doesUserExistCmd, oleDbConn)
            Dim dbread = dbcmd.ExecuteReader()
            If Not dbread.HasRows() Then
                Dim newUserCmd = "INSERT INTO Users (UserName) VALUES('" & User.Identity.Name & "')"
                Dim newUserCmdObj As OleDbCommand = New OleDbCommand(newUserCmd, oleDbConn)
                newUserCmdObj.ExecuteNonQuery()
            End If
        Else
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
            oleDbConn.Open()
            Dim sqlstring As String = _
            "UPDATE Users SET FirstName=@FirstName,LastName=@LastName,Gender=@Gender,Location=@Location,ProfileImage=@ProfileImage WHERE UserName=@UserName"
            Dim dbcmdObj = New OleDbCommand(sqlstring, oleDbConn)

            Dim fileloc = Request.MapPath("userImages") & "\" & User.Identity.Name & ".jpg"
            Me.PictureUpload.SaveAs(fileloc)

            dbcmdObj.Parameters.AddWithValue("@FirstName", FirstName.Text)
            dbcmdObj.Parameters.AddWithValue("@LastName", LastName.Text)
            dbcmdObj.Parameters.AddWithValue("@Gender", Gender.SelectedValue)
            dbcmdObj.Parameters.AddWithValue("@Location", Location.Text)
            dbcmdObj.Parameters.AddWithValue("@ProfileImage", fileloc)
            dbcmdObj.Parameters.AddWithValue("@UserName", User.Identity.Name)

            dbcmdObj.ExecuteNonQuery()

            PlaceHolder1.Controls.Add(New LiteralControl("Congratulations your profile has been updated"))
            Image1.ImageUrl = "userImages/" & User.Identity.Name & ".jpg"
        End If

    End Sub

End Class